def categories2tree(source, parent):
    tree = []
    for item in source:
        if item['p_id'] == parent['id']:
            item['children'] = categories2tree(source, item)
            tree.append(item)
    return tree
